using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure
{
    public class LinkedList02<T>
    {
        private class Node
        {
            public T data;
            public Node next;
            public Node(T data)
            {
                this.data = data;
                this.next = null;
            }
            public Node(T data , Node next)
            {
                this.data = data;
                this.next = next;
            }
            public override string ToString()
            {
                return base.ToString();
            }
        }
        
        private Node head;
        private Node tail;
        private int size;
        
        public LinkedList02()
        {
            this.head = null;
            this.tail = null;
            this.size = 0;
        }
        
        public int Count => size;
        public bool IsEmpty => size == 0;
        
        //链表尾部添加元素
        public void AddLast(T newData)
        {
            Node node = new Node(newData);
            if (IsEmpty)
            {
                head = node;
                tail = node;
            }
            else
            {
                tail.next = node;
                tail = node;
            }
            size++;
        }
        //链表头部删除元素
        public T RemoveFirst()
        {
            if (IsEmpty)
                throw new InvalidOperationException("链表为空");
            
            T deldata = head.data;
            head = head.next;
            size--;
            
            if(head == null)
                tail = null;

            return deldata;
        }
        //查看头部
        public T GetFirst()
        {
            if (IsEmpty)
                throw new InvalidOperationException("链表为空");

            return head.data;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            Node cur = head;
            while (cur != null)
            {
                sb.Append(cur.data + "->");
                cur = cur.next;
            }
            sb.Append("Null");
            return sb.ToString();
        }
    }
}